package com.zhaotianhao.rentingservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.zhaotianhao.rentingservice.api.TypeApi;
import com.zhaotianhao.rentingservice.entity.Renting;
import com.zhaotianhao.rentingservice.entity.Type;
import com.zhaotianhao.rentingservice.mapper.RentingMapper;
import com.zhaotianhao.rentingservice.mapper.RentingRepository;
import com.zhaotianhao.rentingservice.service.IRentingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhaotianhao.rentingservice.vo.RentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Z-7
 * @since 2022-05-16
 */
@Service
public class RentingServiceImpl extends ServiceImpl<RentingMapper, Renting> implements IRentingService {
    @Autowired
    private TypeApi typeApi;
    @Resource
    private RentingRepository rentingRepository;
    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<RentVO> getAll() {
        List<Renting> list = list();
        List<RentVO> rentVOS = new ArrayList<>();
        Object data = typeApi.getAllType().getData();
        List<Type> allType = JSON.parseArray(data.toString(), Type.class);
        list.forEach(rent->{
            RentVO rentVO = new RentVO();
            BeanUtil.copyProperties(rent,rentVO);
            allType.forEach(type->{
                if (rent.getTypeId().equals(type.getId())) {
                    rentVO.setTypeName(type.getTypeName());
                    rentVOS.add(rentVO);
                }
            });
        });
        rentingRepository.deleteAll();
        rentingRepository.saveAll(rentVOS);
        return rentVOS;
    }
}
